package com.tarena.lbs.common.basic.converter;

import com.tarena.lbs.base.protocol.pager.BasePageList;
import com.tarena.lbs.base.protocol.pager.PageResult;
import com.tarena.lbs.pojo.basic.bo.UserBO;
import com.tarena.lbs.pojo.basic.dos.UserDO;
import com.tarena.lbs.pojo.basic.param.UserParam;
import com.tarena.lbs.pojo.basic.query.UserQuery;
import com.tarena.lbs.pojo.basic.vo.UserVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Component
public class UserConverter {

    public UserVO bo2vo(UserBO bo){
        if (null == bo){
            return null;
        }
        UserVO userVO = new UserVO();
        BeanUtils.copyProperties(bo,userVO);
        return userVO;
    }

    public UserDO param2DO(UserParam param){
        if (null == param){
            return null;
        }
        UserDO userDO = new UserDO();
        BeanUtils.copyProperties(param,userDO);
        userDO.setRegTime(new Date());
        userDO.setLastLogTime(new Date());
        userDO.setStatus(0);
        userDO.setUnionId(UUID.randomUUID().toString());
        return userDO;
    }

    public UserBO do2bo(UserDO UserDO) {
        if (null == UserDO){
            return null;
        }
        UserBO userBO = new UserBO();
        BeanUtils.copyProperties(UserDO,userBO);
        return userBO;
    }


    public List<UserBO> doList2boList(List<UserDO> doList ){
        List<UserBO> boList = new ArrayList<>();
        if (CollectionUtils.isEmpty(doList)) {
            return boList;
        }
        for (UserDO userDO : doList) {
            UserBO userBO = new UserBO();
            BeanUtils.copyProperties(userDO,userBO);
            boList.add(userBO);
        }
        return boList;
    }

    public List<UserVO> boList2VoList(List<UserBO> boList ){
        List<UserVO> userVoList = new ArrayList<>();
        if (CollectionUtils.isEmpty(boList)) {
            return userVoList;
        }
        for (UserBO userBO : boList) {
            UserVO userVO = new UserVO();
            BeanUtils.copyProperties(userBO,userVO);
            userVoList.add(userVO);
        }
        return userVoList;
    }

    public PageResult<UserVO> assemblePageVOS(BasePageList<UserBO> bos, UserQuery query) {
        if (bos == null || bos.getTotal() == 0) {
            return null;
        }
        PageResult<UserVO> result = new PageResult<>();
        List<UserVO> vos = new ArrayList<>();
        for (UserBO temp : bos.getList()) {
            UserVO vo = new UserVO();
            BeanUtils.copyProperties(temp,vo);
            vos.add(vo);
        }
        result.setTotal(bos.getTotal());
        result.setObjects(vos);
        result.setPageNo(query.getPageNo());
        result.setPageSize(query.getPageSize());
        return result;
    }

}
